package com.fu.edu.manager.dao;

import com.fu.edu.manager.domain.Student;

import java.util.ArrayList;

/**
 * @author 付大大
 * @create 2021-01-06 10:53
 */
public class OtherStudentDao implements BaseStudentDao {
    private static ArrayList<Student> students = new ArrayList<>();

    static {
        Student student1 = new Student("2006910242", "付大大", "23", "20170101");
        Student student2 = new Student("2006910241", "周大大", "23", "20170101");
        students.add(student1);
        students.add(student2);

    }

    public boolean addStudent(Student student) {
        return students.add(student);
    }

    public Student[] findAllStudent() {
        Student[] student = new Student[OtherStudentDao.students.size()];
        for (int i = 0; i < student.length; i++) {
            student[i] = students.get(i);
        }
        return student;
    }

    public void deleteStudentById(String id) {
        int index = getIndex(id);
        students.remove(index);
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student != null && student.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(Student student, String id) {
        int index = getIndex(id);
        students.set(index, student);
    }
}
